package com.lin.healthmanagement.controller;

import com.lin.healthmanagement.controller.code.Code;
import com.lin.healthmanagement.controller.code.Message;
import com.lin.healthmanagement.controller.exception.BusinessException;
import com.lin.healthmanagement.controller.exception.SystemException;
import com.lin.healthmanagement.controller.result.Result;
import com.lin.healthmanagement.dao.UserDao;
import com.lin.healthmanagement.entity.QueryInfo;
import com.lin.healthmanagement.entity.User;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserDao userDao;

    @GetMapping("/allUsers")
    public Result getUserList(QueryInfo queryInfo){
        String queryInfoBlurred="%"+queryInfo.getUserName()+"%";
        List<User> users=new ArrayList<>();
        Integer numbers=null;
        Integer code=Code.SYSTEM_BUSY;
        String msg= Message.Error;
        try {
            if(queryInfo.getPageCurrent()<=0){
                throw new BusinessException(Code.SYSTEM_BUSY,"当前页不能小于等于0");
            }
            numbers=userDao.getUsersCounts(queryInfoBlurred);
            int pageStart=(queryInfo.getPageCurrent()-1)*queryInfo.getPageSize();
            users=userDao.getAllUsers(queryInfoBlurred,pageStart,queryInfo.getPageSize());
            if (users != null){
                code=Code.GET_OK;
                msg=Message.ok;
            }
        } catch (BusinessException e) {
            throw new BusinessException(code,msg);
        }catch (Exception e){
            throw e;
        }

        HashMap<String,Object> united=new HashMap<>();
        united.put("numbers",numbers);
        united.put("userList",users);

        return new Result(code,msg,united);
    }


    @PutMapping("/userState")
    public Result updateUserState(@RequestParam("id")Integer id,
                                  @RequestParam("state")Integer state){
        int i=0;
        Integer code=Code.SYSTEM_ERR;
        String msg=Message.Error;

        try {
            i=userDao.updateState(id,state);
            if(i>=1){
                code=Code.UPDATE_OK;
                msg=Message.ok;
            }
        } catch (SystemException e) {
            throw new SystemException(Code.UPDATE_ERR,Message.fail);
        }catch (Exception e){
            throw e;
        }

        return new Result(code,msg,null);
    }


    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
        Integer code=Code.SYSTEM_ERR;
        String msg=Message.Error;
        try {
            user.setRole("普通用户");
            user.setDeleteOr(0);
            int i=userDao.addUser(user);
            if(i>=0){
                code=Code.SAVE_OK;
                msg=Message.ok;
            }
            else {
                code=Code.SAVE_ERR;
                msg=Message.fail;
            }
        } catch (SystemException e) {
            throw new SystemException(Code.SYSTEM_ERR,Message.Error);
        }catch (Exception e){
            throw e;
        }

        return new Result(code,msg,null);
    }


    @DeleteMapping("/deleteUser")
    public Result deleteById(@RequestParam("id")int id){
        Integer code=Code.SYSTEM_ERR;
        String msg=Message.Error;
        try {
            int i=userDao.deleteById(id);
            if(i>0){
                code=Code.DELETE_OK;
                msg=Message.ok;
            }
            else {
                code=Code.DELETE_ERR;
                msg=Message.fail;
            }
        } catch (SystemException e) {
            throw new SystemException(code,msg);
        }catch (Exception e){
            throw e;
        }

        return  new Result(code,msg,null);
    }

    @GetMapping("/getUpdate")
    public Result getUpdateUser(@RequestParam("id")int id){
        Integer code=Code.SYSTEM_ERR;
        String msg=Message.Error;
        User user=null;

        try {
            user=userDao.getUpdateUser(id);
            if(user!=null){
                code=Code.GET_OK;
                msg=Message.ok;
            }else {
                code=Code.GET_ERR;
                msg=Message.fail;
            }
        }catch (SystemException e) {
            throw new SystemException(code,msg);
        }catch (Exception e){
            throw e;
        }

        return  new Result(code,msg,user);
    }

    @PutMapping("/editUser")
    public Result editUser(@RequestBody User user){
        Integer code=Code.SYSTEM_ERR;
        String msg=Message.Error;

        try {
            int i=userDao.updateUser(user);
            if(i>0){
                code=Code.UPDATE_OK;
                msg=Message.ok;
            }else {
                code=Code.UPDATE_ERR;
                msg=Message.fail;
            }
        }catch (SystemException e) {
            throw new SystemException(code,msg);
        }catch (Exception e){
            throw e;
        }

        return  new Result(code,msg,null);
    }




}
